package com.yannuo.controller;

import com.yannuo.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
Springmvc执行流程：
1.用户发送请求-->DispatherServlet（前端控制器）,前端控制器自己不处理，而是委托给其他解析器进行处理.

2.DispatherServlet（前端控制器）-->HandleMapping(映射处理器)-->将请求映射到HandlerExecutionChain->前端控制器

3.DispatherServlet（前端控制器）-->HandleAdapter(适配处理器)-->调用相应的处理器方法(Controller)->前端控制器

4.DispatherServlet（前端控制器）-->视图解析器-->返回一个View对象->前端控制器

5.DispatherServlet（前端控制器）-->页面渲染->前端控制器->响应资源到浏览器
*/
@RestController
//@Controller
//@RequestMapping("/user")//代表一级访问路径
public class UserController {

    @RequestMapping("/save")
//    @ResponseBody
    public String save(){
        System.out.println("user save......");
        return "{'info':'hello,springmvc.............'}";
    }

    @RequestMapping("del")
//    @ResponseBody
    public String del(){
        System.out.println("user del......");
        return "{'info':'hehe,springmvc.............'}";
    }

    @RequestMapping("pbind")
    public String paramBind(@RequestParam("uname") String username, String password){
        System.out.println("come........");
        return username+","+password;
    }


    @RequestMapping("/pojoBind")
    public String pojoBind(HttpServletRequest request, HttpServletResponse response){
        //需求：绑定对象
        String id = request.getParameter("aaa");
        String name = request.getParameter("bbb");
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setName(name);
        return id+","+name;
    }

    @RequestMapping("/pojoBind1")
    public String pojoBind1(User user){
        //需求：绑定对象
        return user.getId()+","+user.getName();
    }

    @RequestMapping("/arrayBind")
    public String arrayBind(String[] hobby){
        //需求：绑定对象
        return hobby[0]+","+hobby[1];
    }
//    @RequestMapping(method = RequestMethod.POST)
    @PostMapping("/likeUsers")
    @ResponseBody
    public String handleUser(@RequestBody  User user){
        return "userInfo:"+user.toString();
    }

    @RequestMapping("/testUser")
    public String CoreTest(User user){
        System.out.println("user = " + user);
        return "success";
//        return "userInfo:"+user.toString();
    }

    @RequestMapping("/listPojoParamForJson")
    @ResponseBody
    public String listPojoParamForJson(@RequestBody List<User> list){
        System.out.println("list ==> "+list);
        return "success...";
    }

    @GetMapping("/toJsonUsers")
    public List<User> respBrowserData(){
        List<User> users = new ArrayList<>();
        User user1 = new User(1001,"zhangsan1");
        User user2 = new User(1022,"zhangsan2");
        User user3 = new User(1521,"zhangsan3");
        User user4 = new User(1311,"zhangsan4");
        User user5 = new User(1981,"zhangsan5");
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        users.add(user5);
        return users;
    }

    @GetMapping("/toDate")
    public String toDateData(Date date,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date2){
        System.out.println("parameter passing date:"+date);
        System.out.println("parameter passing date1:"+date1);
        System.out.println("parameter passing date2:"+date2);
        return "date data success.";
    }
}
